﻿namespace AzureLogProviders.RollingXmlTraceListener.Configuration.Fluent
{
    using System;
    using System.Diagnostics;

    using Microsoft.Practices.EnterpriseLibrary.Common.Configuration.Fluent;
    using Microsoft.Practices.EnterpriseLibrary.Common.Properties;
    using Microsoft.Practices.EnterpriseLibrary.Logging.Configuration;
    using Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners;

    /// <summary>
    /// Extension methods to support configuration of <see cref="RollingFlatFileTraceListener"/>.
    /// </summary>
    public static class SendToRollingXmlFileTraceListenerExtensions
    {
        /// <summary>
        /// Adds a new <see cref="RollingFlatFileTraceListener"/> to the logging settings and creates
        /// a reference to this Trace Listener for the current category source.
        /// </summary>
        /// <param name="context">Fluent interface extension point.</param>
        /// <param name="listenerName">The name of the <see cref="RollingFlatFileTraceListener"/>.</param>
        /// <returns>Fluent interface that can be used to further configure the created <see cref="RollingFlatFileTraceListenerData"/>. </returns>
        /// <seealso cref="RollingFlatFileTraceListenerData"/>
        public static ILoggingConfigurationSendToRollingXmlFileTraceListener RollingFile(this ILoggingConfigurationSendTo context, string listenerName)
        {
            if (string.IsNullOrEmpty(listenerName))
            {
                throw new ArgumentException(Resources.ExceptionStringNullOrEmpty, "listenerName");
            }

            return new SendToRollingXmlFileTraceListenerBuilder(context, listenerName);
        }

        private class SendToRollingXmlFileTraceListenerBuilder : SendToTraceListenerExtension, ILoggingConfigurationSendToRollingXmlFileTraceListener
        {
            private RollingXmlTraceListenerData rollingTraceListenerData;

            public SendToRollingXmlFileTraceListenerBuilder(ILoggingConfigurationSendTo context, string listenerName)
                : base(context)
            {
                this.rollingTraceListenerData = new RollingXmlTraceListenerData()
                {
                    Name = listenerName
                };

                base.AddTraceListenerToSettingsAndCategory(this.rollingTraceListenerData);
            }

            public ILoggingConfigurationSendToRollingXmlFileTraceListener RollEvery(RollInterval interval)
            {
                this.rollingTraceListenerData.RollInterval = interval;
                
                return this;
            }

            public ILoggingConfigurationSendToRollingXmlFileTraceListener WhenRollFileExists(RollFileExistsBehavior behavior)
            {
                this.rollingTraceListenerData.RollFileExistsBehavior = behavior;

                return this;
            }

            public ILoggingConfigurationSendToRollingXmlFileTraceListener RollAfterSize(int rollSizeInKB)
            {
                this.rollingTraceListenerData.RollSizeKB = rollSizeInKB;

                return this;
            }

            public ILoggingConfigurationSendToRollingXmlFileTraceListener UseTimeStampPattern(string timeStampPattern)
            {
                this.rollingTraceListenerData.TimeStampPattern = timeStampPattern;

                return this;
            }

            public ILoggingConfigurationSendToRollingXmlFileTraceListener ToFile(string filename)
            {
                this.rollingTraceListenerData.FileName = filename;

                return this;
            }

            public ILoggingConfigurationSendToRollingXmlFileTraceListener Filter(SourceLevels sourceLevel)
            {
                this.rollingTraceListenerData.Filter = sourceLevel;

                return this;
            }

            public ILoggingConfigurationSendToRollingXmlFileTraceListener WithTraceOptions(TraceOptions traceOptions)
            {
                this.rollingTraceListenerData.TraceOutputOptions = traceOptions;

                return this;
            }

            public ILoggingConfigurationSendToRollingXmlFileTraceListener CleanUpArchivedFilesWhenMoreThan(int maximumArchivedFiles)
            {
                this.rollingTraceListenerData.MaxArchivedFiles = maximumArchivedFiles;

                return this;
            }
        }

    }
}
